package org.zjx.service;

import org.zjx.common.entity.Address;
import org.zjx.common.bean.Role;
import org.zjx.common.service.LoadPlayerStateService;
import org.zjx.db.RoleDB;
import org.zjx.server.annotation.RpcService;
import org.zjx.state.PlayerState;
import org.zjx.state.StateManager;

@RpcService(LoadPlayerStateService.class)
public class LoadPlayerStateServiceImpl implements LoadPlayerStateService {
    @Override
    public Address getAddress(String playerId) {
        PlayerState playerState = StateManager.getInstance().getPlayerState(playerId);
        if (playerState == null) return null;
        return playerState.getAddress();
    }

    @Override
    public Long getStandStartTime(String playerId) {
        PlayerState playerState = StateManager.getInstance().getPlayerState(playerId);
        if (playerState == null) return null;
        return playerState.getStandStartTime();
    }

    @Override
    public Integer getHp(String playerId) {
        PlayerState playerState = StateManager.getInstance().getPlayerState(playerId);
        if (playerState == null) return null;
        return playerState.getHp();
    }

    @Override
    public Integer getInjury(String playerId) {
        PlayerState playerState = StateManager.getInstance().getPlayerState(playerId);
        if (playerState == null) return null;
        return playerState.getInjury();
    }

    @Override
    public Integer getMp(String playerId) {
        PlayerState playerState = StateManager.getInstance().getPlayerState(playerId);
        if (playerState == null) return null;
        return playerState.getHp();
    }

    @Override
    public Integer getMaxHp(String playerId) {
        Role role = new RoleDB().findByRoleId(playerId);
        if (role == null) return null;
        return role.getMaxHp();
    }

    @Override
    public Integer getMaxMp(String playerId) {
        Role role = new RoleDB().findByRoleId(playerId);
        if (role == null) return null;
        return role.getMaxMp();
    }

    @Override
    public Double getBloodPercentage(String playerId) {
        Integer hp = getHp(playerId);
        Integer maxHp = getMaxHp(playerId);

        if (hp == null || maxHp == null) return null;

        if (maxHp == 0) {
            return 0.0; // Avoid division by zero
        }

        double percentage = (hp * 100.0) / maxHp;
        return Math.round(percentage * 100) / 100.0;
    }

    @Override
    public Double getManaPercentage(String playerId) {
        Integer mp = getMp(playerId);
        Integer maxMp = getMaxMp(playerId);

        if (mp == null || maxMp == null) return null;

        if (maxMp == 0) {
            return 0.0;
        }

        double percentage = (mp * 100.0) / maxMp;
        return Math.round(percentage * 100) / 100.0;
    }

    public Long getDemonIncense(String playerId) {
        PlayerState playerState = StateManager.getInstance().getPlayerState(playerId);
        return playerState != null ? playerState.getDemonIncense() : null;
    }

    public Integer getLevel(String playerId) {
        Role role = new RoleDB().findByRoleId(playerId);
        return role != null ? role.getLevel() : null;
    }

    public Integer getVitality(String playerId) {
        PlayerState playerState = StateManager.getInstance().getPlayerState(playerId);
        return playerState != null ? playerState.getVitality() : null;
    }

    public Integer getMaxVitality(String playerId) {
        Role role = new RoleDB().findByRoleId(playerId);
        return role != null ? role.getVitality() : null;
    }

    public Integer getPhysical(String playerId) {
        PlayerState playerState = StateManager.getInstance().getPlayerState(playerId);
        return playerState != null ? playerState.getPhysical() : null;
    }

    public Integer getMaxPhysical(String playerId) {
        Role role = new RoleDB().findByRoleId(playerId);
        return role != null ? role.getPhysical() : null;
    }

    public Integer getPopularity(String playerId) {
        Role role = new RoleDB().findByRoleId(playerId);
        return role != null ? role.getPopularity() : null;
    }

    public String getGangName(String playerId) {
        Role role = new RoleDB().findByRoleId(playerId);
        return role != null ? role.getGangName() : null;
    }

    public Long getGangNumber(String playerId) {
        Role role = new RoleDB().findByRoleId(playerId);
        return role != null ? role.getGangNumber() : null;
    }

    public Integer getGangValue(String playerId) {
        Role role = new RoleDB().findByRoleId(playerId);
        return role != null ? role.getGangValue() : null;
    }

    public Integer getSectValue(String playerId) {
        Role role = new RoleDB().findByRoleId(playerId);
        return role != null ? role.getSectValue() : null;
    }

    public Integer getPotential(String playerId) {
        Role role = new RoleDB().findByRoleId(playerId);
        return role != null ? role.getPotential() : null;
    }

    public Integer getExperience(String playerId) {
        Role role = new RoleDB().findByRoleId(playerId);
        return role != null ? role.getExperience() : null;
    }

    public Integer getRage(String playerId) {
        Role role = new RoleDB().findByRoleId(playerId);
        return role != null ? role.getRage() : null;
    }

    public Integer getCash(String playerId) {
        Role role = new RoleDB().findByRoleId(playerId);
        return role != null ? role.getCash() : null;
    }

    public Integer getFund(String playerId) {
        Role role = new RoleDB().findByRoleId(playerId);
        return role != null ? role.getFund() : null;
    }

    public Integer getDeposit(String playerId) {
        Role role = new RoleDB().findByRoleId(playerId);
        return role != null ? role.getDeposit() : null;
    }

    public Integer getMaxPetValue(String playerId) {
        Role role = new RoleDB().findByRoleId(playerId);
        return role != null ? role.getMaxPetValue() : null;
    }
}
